import {
  get,
  post,
  put,
  del,
} from "./base_service"


/**
 * 佣金相关
 *
 * allen  2021-08-24
 */



/**
 * 新增佣金
 * @param {*} data 
 */
const createCommissionForHouse = (data) => {
  return new Promise((resolve, reject) => {
    post("/manager/commission", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询当前房源佣金列表
 * @param {*} houseNumber 
 */
const queryHouseCommissionList = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get("/manager/commission?houseNumber=" + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 更新房源佣金
 * @param {*} data 
 */
const updateCommissionForHouse = (data) => {
  return new Promise((resolve, reject) => {
    put("/manager/commission", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 删除一个佣金
 * @param {*} id 
 */
const deleteCommissionForHouse = (id) => {
  return new Promise((resolve, reject) => {
    del("/manager/commission?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询佣金规则列表
 */
const queryCommissionRules = () => {
  return new Promise((resolve, reject) => {
    get("/manager/commission/rule").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 设置分佣
 * @param {*} data 
 */
const settingHouseDistributionCommission = (data) => {
  return new Promise((resolve, reject) => {
    post("/manager/recommend/give/commission", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 获取已分配佣金详情
 * @param {*} recommendId 
 */
const settingHouseDistributionCommissionDetail = (recommendId) => {
  return new Promise((resolve, reject) => {
    get("/manager/recommend/give/commission/detail?recommendId=" + recommendId).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })

}


/**
 * 查询已转账的佣金信息
 * @param {*} id 
 */
const queryTransferCommissionDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/manager/recommend/transfer/commission/detail?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })

}

/**
 * 提交转账记录凭证
 * @param {*} data 
 */
const pushTransferCommissionInfo = (data) => {
  return new Promise((resolve, reject) => {
    post("/manager/recommend/commission/transfer", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })

}


/**
 * 搜索佣金
 * @param {*} words 
 */
const searchBindCommissionList = (words) => {
  return new Promise((resolve, reject) => {
    post("/manager/commission/search/commission", { words: words }).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 搜索佣金
 * @param {*} houseNumber
 */
const searchBindHouseCommissionList = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get("/manager/commission/query/had/commission?houseNumber=" + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}



/**
 * 绑定佣金
 * @param {*} data 
 */
const bindCommissionToHouse = (data) => {
  return new Promise((resolve, reject) => {
    post("/manager/commission/bind/commission", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询公司总业绩
 */
const queryCompanyTotalPerformance = () => {
  return new Promise((resolve, reject) => {
    get("/performance/company").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 公司查询个人的业绩
 * @param {*} wxUserId 
 * @param {*} companyNumber 
 * @param {*} settlementStatus 
 */
const queryPersonalCompanyPer = (wxUserId, companyNumber, settlementStatus) => {
  return new Promise((resolve, reject) => {
    get("/performance/query/employee?wxUserId=" + wxUserId + "&companyNumber=" + companyNumber + "&settlementStatus=" + settlementStatus).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

module.exports = {
  createCommissionForHouse,
  queryHouseCommissionList,
  updateCommissionForHouse,
  deleteCommissionForHouse,
  queryCommissionRules,
  settingHouseDistributionCommission,
  settingHouseDistributionCommissionDetail,
  queryTransferCommissionDetail,
  pushTransferCommissionInfo,
  searchBindCommissionList,
  bindCommissionToHouse,
  searchBindHouseCommissionList,
  queryCompanyTotalPerformance,
  queryPersonalCompanyPer
}
